// 八字命理功能
function initBaziModule() {
  const baziForm = document.getElementById('baziForm');
  if (!baziForm) return;
  
  baziForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    // 检查登录状态
    if (checkNeedLogin()) return;
    
    const birthYear = document.getElementById('birthYear').value;
    const birthMonth = document.getElementById('birthMonth').value;
    const birthDay = document.getElementById('birthDay').value;
    const birthHour = document.getElementById('birthHour').value;
    
    // 生成八字
    baziAPI.generateBazi(userToken, birthYear, birthMonth, birthDay, birthHour)
      .then(response => {
        if (response.code === 200) {
          // 显示基础分析结果
          showBaziResult(response.data);
          
          // 保存八字ID用于后续查询
          document.getElementById('baziId').value = response.data.baziId;
        } else {
          showToast(response.message || '生成八字失败');
        }
      })
      .catch(error => {
        showToast(error.message || '生成八字失败');
      });
  });
  
  // 查看详细分析按钮
  const viewDetailBtn = document.getElementById('viewBaziDetailBtn');
  if (viewDetailBtn) {
    viewDetailBtn.addEventListener('click', function() {
      const baziId = document.getElementById('baziId').value;
      if (!baziId) {
        showToast('请先生成八字');
        return;
      }
      
      // 检查是否需要支付
      if (checkNeedPayment(false, 1.1)) {
        // 设置支付相关信息
        document.getElementById('paymentType').value = 'bazi';
        document.getElementById('productId').value = baziId;
        return;
      }
      
      // 获取详细分析
      baziAPI.getBaziDetail(userToken, baziId)
        .then(response => {
          if (response.code === 200) {
            showBaziDetailResult(response.data);
          } else {
            showToast(response.message || '获取详细分析失败');
          }
        })
        .catch(error => {
          showToast(error.message || '获取详细分析失败');
        });
    });
  }
}

// 显示八字基础分析结果
function showBaziResult(data) {
  const resultContainer = document.getElementById('baziResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const baziInfo = document.getElementById('baziInfo');
  const baziAnalysis = document.getElementById('baziAnalysis');
  
  if (baziInfo) {
    baziInfo.innerHTML = `
      <p class="text-lg font-medium">您的八字：${data.bazi}</p>
      <p class="mt-2">日主：${data.dayMaster}</p>
      <p>五行：${data.fiveElements}</p>
      <p>纳音：${data.nayin}</p>
    `;
  }
  
  if (baziAnalysis) {
    baziAnalysis.innerHTML = `
      <p class="mt-4">${data.basicAnalysis}</p>
      <p class="mt-2 text-gray-500">查看详细分析可获得更多命运轨迹解读</p>
    `;
  }
}

// 显示八字详细分析结果
function showBaziDetailResult(data) {
  const detailContainer = document.getElementById('baziDetailContainer');
  if (!detailContainer) return;
  
  detailContainer.style.display = 'block';
  
  const detailContent = document.getElementById('baziDetailContent');
  if (detailContent) {
    detailContent.innerHTML = `
      <h3 class="text-xl font-bold mb-4">详细命运轨迹分析</h3>
      <div class="space-y-4">
        <div>
          <h4 class="text-lg font-medium text-indigo-700">性格特点</h4>
          <p>${data.personality}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">事业发展</h4>
          <p>${data.career}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">财运分析</h4>
          <p>${data.wealth}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">健康状况</h4>
          <p>${data.health}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">人际关系</h4>
          <p>${data.relationships}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">吉凶方位</h4>
          <p>${data.directions}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">运势变化</h4>
          <p>${data.fortune}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">改运建议</h4>
          <p>${data.suggestions}</p>
        </div>
      </div>
    `;
  }
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
  initBaziModule();
});
